package com.google.demo.view;


import com.google.demo.R;
import com.google.demo.Util.Util;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.TextView;

public class AnimValidation {
	private EditText mEditText1;
	private EditText mEditText2;
	private TextView mTextView1;
	private TextView mTextView2;
	private Context mContext;
	private int option;
	public AnimValidation(Context context,int option,EditText editText1,EditText editText2,TextView textView1,TextView textView2) {
		// TODO Auto-generated constructor stub
		this.mContext=context;
		this.mEditText1=editText1;
		this.mEditText2=editText2;
		this.mTextView1=textView1;
		this.mTextView2=textView2;
		this.option=option;
	}
	private EditText mEditText3;
	private EditText mEditText4;
	private EditText mEditText5;
	private TextView mTextView3;
	private TextView mTextView4;
	private TextView mTextView5;
	public AnimValidation(Context context,int option,EditText editText3,EditText editText4,EditText editText5,TextView textView3,TextView textView4,TextView textView5) {
		// TODO Auto-generated constructor stub
		this.mContext=context;
		this.mEditText3=editText3;
		this.mEditText4=editText4;
		this.mEditText5=editText5;
		this.mTextView3=textView3;
		this.mTextView4=textView4;
		this.mTextView5=textView5;
		this.option=option;
	}
	private EditText mEditText6;
	private TextView mTextView6;
	public AnimValidation(Context context,int option,EditText editText6,TextView textView6) {
		// TODO Auto-generated constructor stub
		this.mContext=context;
		
		this.mEditText6=editText6;
		this.mTextView6=textView6;
		
		this.option=option;
	}
	public void validAnim(){
		switch (option) {
		case 0:
			mEditText1.setOnFocusChangeListener(new AnimValid());
			mEditText2.setOnFocusChangeListener(new AnimValid());
			mEditText1.addTextChangedListener(passWatcher);
			mEditText2.addTextChangedListener(userWatcher);
			break;
		case 1:
			mEditText3.setOnFocusChangeListener(new AnimValid());
			mEditText4.setOnFocusChangeListener(new AnimValid());
			mEditText5.setOnFocusChangeListener(new AnimValid());
			mEditText3.addTextChangedListener(txtWatcher3);
			mEditText4.addTextChangedListener(txtWatcher4);
			mEditText5.addTextChangedListener(txtWatcher5);
			break;
		case 2:
			mEditText6.setOnFocusChangeListener(new AnimValid());			
			mEditText6.addTextChangedListener(txtWatcher6);
			break;
		default:
			break;
		}
		
	}
	private class AnimValid implements OnFocusChangeListener{

		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			// TODO Auto-generated method stub
			Animation shake = AnimationUtils.loadAnimation(
					mContext, R.anim.shake);
			switch (option) {
			case 0:
				if (v == mEditText1 && hasFocus == false) {
					String username = mEditText1.getText().toString();
					if (Util.isStringEmpty(username) || username.length() < 6) {
						mEditText1.startAnimation(shake);
						setError(mEditText1,mTextView1, true);
					} else {
						
						setError(mEditText1,mTextView1, false);
					}
				} else if (v == mEditText2 && hasFocus == false) {
					String pass = mEditText2.getText().toString();
					if (Util.isStringEmpty(pass) || pass.length() < 6) {
						mEditText2.startAnimation(shake);
						setError(mEditText2,mTextView2, true);
					} else {
						
						setError(mEditText2,mTextView2, false);
					}
				}
				break;
			case 1:
				if (v == mEditText3 && hasFocus == false) {
					String txt3 = mEditText3.getText().toString();
					if (Util.isStringEmpty(txt3) || txt3.length() < 6) {
						mEditText3.startAnimation(shake);
						setError(mEditText3,mTextView3, true);
					} else {
						setError(mEditText3,mTextView3, false);
					}
				} else if (v == mEditText4 && hasFocus == false) {
					String txt4 = mEditText4.getText().toString();
					if (Util.isStringEmpty(txt4) || txt4.length() < 6) {
						mEditText4.startAnimation(shake);
						setError(mEditText4,mTextView4, true);
					} else {
						setError(mEditText4,mTextView4, false);
					}
				}
				else if (v == mEditText5 && hasFocus == false) {
					String txt5 = mEditText5.getText().toString();
					if (Util.isStringEmpty(txt5) || txt5.length() < 6) {
						mEditText5.startAnimation(shake);
						setError(mEditText5,mTextView5, true);
					} else {
						setError(mEditText4,mTextView4, false);
					}
				}
				break;
			case 2:
				if (v == mEditText6 && hasFocus == false) {
					String username = mEditText6.getText().toString();
					if (Util.isStringEmpty(username) || username.length() < 6) {
						mEditText6.startAnimation(shake);
						setError(mEditText6,mTextView6, true);
					} else {
						setError(mEditText6,mTextView6, false);
					}
				} 
				break;
			default:
				break;
			}
			
		}
		
		
	}
	private void setError(EditText editText,TextView textView, boolean isError) {
		if (isError){
			textView.setVisibility(View.INVISIBLE);
			editText.setTextColor(Color.RED);
		}
			
		else{
			textView.setVisibility(View.VISIBLE);
			editText.setTextColor(Color.BLACK);
		}
			
	}
	private TextWatcher passWatcher = new TextWatcher() {

		
		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			mTextView2.setVisibility(View.INVISIBLE);
			
			setError(mEditText2,mTextView2, false);
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void afterTextChanged(Editable s) {
		}
	};
	private TextWatcher userWatcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			mTextView1.setVisibility(View.INVISIBLE);
			
			setError(mEditText1,mTextView1, false);
			
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void afterTextChanged(Editable s) {
		}
	};
	/////////////////////////////////
	private TextWatcher txtWatcher3 = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			mTextView3.setVisibility(View.INVISIBLE);
			setError(mEditText3,mTextView3, false);
			
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void afterTextChanged(Editable s) {
		}
	};
	private TextWatcher txtWatcher4 = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			mTextView4.setVisibility(View.INVISIBLE);
			setError(mEditText4,mTextView4, false);
			
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void afterTextChanged(Editable s) {
		}
	};
	private TextWatcher txtWatcher5 = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			mTextView5.setVisibility(View.INVISIBLE);
			setError(mEditText5,mTextView5, false);
			
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void afterTextChanged(Editable s) {
		}
	};
	private TextWatcher txtWatcher6 = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			mTextView6.setVisibility(View.INVISIBLE);
			setError(mEditText6,mTextView6, false);
			
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void afterTextChanged(Editable s) {
		}
	};
}
